// pages/mall/noticeDetail/index.js
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
const $api = require('../../../../request/api').API;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    beeperId: '',
    status: '',
    notifyId: '',
    operateList: [
      {
        icon: 'orders-o',
        text: '通知列表',
        type: 1
      },
      {
        icon: 'phone',
        text: '一键重播',
        type: 2
      }
    ],
    info: {},
    actions: [
      {
        name: '口头通知'
      },
      {
        name: '电话通知'
      }
    ],
    show: false,
    taskRecordId: '',
    phones: [],
    phoneShow: false,
    statisticsList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let operateList = this.data.operateList;
    if (options.notifyId) {
      operateList.splice(0, 1);
    }
    this.setData({
      beeperId: options.beeperId,
      status: options.identity,
      notifyId: options.notifyId || '',
      operateList: operateList
    });
    this.getDetail();
  },
  typeFilter(val) {
    switch (val) {
      case 'fire':
        val = '火警';
        break;

      case 'gasLeak':
        val = '燃气泄漏';
        break;

      case 'lockOpen':
        val = '被盗风险';
        break;
      case 'soak':
        val = '水浸溢水';
        break;
      case 'sos':
        val = '紧急求助';
        break;
      case 'fall':
        val = '洪涝灾害';
        break;
    }
    return val;
  },
  getDetail() {
    let params = {};
    if (this.data.notifyId) {
      params.notifyId = this.data.notifyId;
      $api.noticeDetail_Id(params).then(res => {
        if (res.data.status == 0) {
          let data = res.data.data;
          data.typeName = this.typeFilter(data.notifyType);
          this.setData({
            info: data
          });
        }
      });
      this.getStatisticsList(this.data.notifyId);
    } else {
      params.beeperId = this.data.beeperId;
      $api.noticeDetail(params).then(res => {
        if (res.data.status == 0) {
          let data = res.data.data;
          data.typeName = this.typeFilter(data.notifyType);
          this.setData({
            info: data
          });
          this.getStatisticsList(this.data.info.notifyId);
        }
      });
    }
  },
  getStatisticsList(notifyId) {
    $api
      .statisticsList({
        notifyId: notifyId
      })
      .then(res => {
        if (res.data.status == 0) {
          let data = res.data.data;
          const keyOrder = ['sumHouse', 'successHouse', 'notifyPhone', 'connectedPhone'];
          const dataArray = keyOrder.map(key => {
            let name;
            switch (key) {
              case 'sumHouse':
                name = '通知户数';
                break;
              case 'successHouse':
                name = '成功户数';
                break;
              case 'notifyPhone':
                name = '通知人数';
                break;
              case 'connectedPhone':
                name = '接通人数';
                break;
              default:
                name = 'Unknown';
            }
            return {
              name,
              value: data[key]
            };
          });
          this.setData({
            statisticsList: dataArray
          });
        }
      });
  },
  onClose() {
    this.setData({
      show: false
    });
  },

  onSelect(event) {},

  getMx() {
    $api
      .noticeMx({
        notifyId: this.data.info.notifyId
      })
      .then(res => {
        if (res.data.status == 0) {
          Toast.success('更新成功');
          let data = res.data.data;
          this.setData({
            'info.taskRecordList': data
          });
        }
      });
    this.getStatisticsList(this.data.info.notifyId);
  },
  noticeClick(e) {
    let { id } = e.currentTarget.dataset;
    this.setData({
      taskRecordId: id
    });
    Dialog.confirm({
      title: '提示',
      message: '是否确定已通知？'
    })
      .then(() => {
        $api
          .offlineNotice({
            taskRecordId: this.data.taskRecordId
          })
          .then(res => {
            if (res.data.status == 0) {
              Toast.success('操作成功');
              this.getDetail();
            }
          });
      })
      .catch(() => {
        // on cancel
      });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  operateClick(e) {
    let { type } = e.currentTarget.dataset;
    // 1列表 2重试
    switch (type) {
      case 1:
        wx.navigateTo({
          url: '/subcontract/home/pages/noticeList/index?beeperId=' + this.data.beeperId
        });
        break;

      case 2:
        $api
          .onekeyReport({
            notifyId: this.data.info.notifyId
          })
          .then(res => {
            if (res.data.status == 0) {
              Toast.success('操作成功');
              setTimeout(() => {
                this.getDetail();
              }, 1000);
            }
          });

        break;
    }
  },
  openPhone(e) {
    let phones = e.currentTarget.dataset.item.phones;
    if (!phones) {
      Toast.fail('暂无可拨打电话号码');
      return;
    }
    if (phones.includes(',')) {
      phones = phones.split(',');
      phones = phones.map(item => ({
        name: item
      }));
    } else {
      phones = [
        {
          name: phones
        }
      ];
    }
    this.setData({
      phones,
      phoneShow: true
    });
  },
  phoneClose() {
    this.setData({
      phoneShow: false
    });
  },
  phoneCall(event) {
    wx.makePhoneCall({
      phoneNumber: event.detail.name,
      success: function () {
        console.log('拨打电话成功！');
      },
      fail: function () {
        console.log('拨打电话失败！');
      }
    });
  }
});
